package com.bootcamp.s2_0922.ds.Exercise._0930_Ex;

/**
 * @program: myClassCode
 * @description:
 * @author: Keyu Li
 * @create: 2021-09-30 19:50
 **/

public class Ex3_CountLetter {
    public static void main(String[] args) {
        String s = "aababcabcdabcde";
        // 使用数组接收解析后的 字母：频次
        int[] letterFreq = calLetterFreq(s);
        // 将该数组打印到控制台
        printLetterFreq(letterFreq);
    }

    private static void printLetterFreq(int[] letterFreq) {
        for (int i = 0; i < letterFreq.length; i++) {
            if (letterFreq[i] == 0) continue;
            System.out.print(((char)( i + 'a')) + "(" + letterFreq[i] + ")");
        }
        System.out.println();
    }

    private static int[] calLetterFreq(String s) {
        // 定义一个 a-z 数组存放每个字母
        int[] letterFreq = new int[26];
        // 将 chars 按照字母下标存档到该数组
        for (int i = 0; i < s.length(); i++) {
            char temp = s.charAt(i);
            letterFreq[temp - 'a']++;
        }
        // 返回该数组
        return letterFreq;
    }
}
